package net.lanelife.casino.cms.web.controller.support;

import java.util.List;

import javax.servlet.ServletRequest;

import net.lanelife.casino.caipiao.service.api.IUserService;
import net.lanelife.casino.common.repository.SearchFilter;
import net.lanelife.casino.common.utils.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserSearchFilter {
	
	public static final String FILTER_TYPE_PARAM_NAME = "userSearchType";
	public static final String FILTER_USERNAME_PARAM_NAME = "userName";
	
	@Autowired
	private IUserService userService;
	
	public static enum FilterType {
		SELF, TEAM
	}
	
	public void buildSearchFilter(List<SearchFilter> filters, ServletRequest request) {
		
		FilterType searchType = FilterType.valueOf(request.getParameter(FILTER_TYPE_PARAM_NAME));	// 获取查询类型
		String userName = request.getParameter(FILTER_USERNAME_PARAM_NAME); 						// 获取查询的用户帐号
		
		if (StringUtils.isNotBlank(userName)) {
			switch (searchType) {
			case TEAM: // 按团队查询
				String path = userService.getPath(userName);
				filters.add(new SearchFilter("path", SearchFilter.Operator.ALIKE, path));
				break;
			default:   // 按个人查询
				filters.add(new SearchFilter("userName", SearchFilter.Operator.EQ, userName));
				break;
			}
		}
		
	}

}
